// Register bookmark_buttons
BloggerExt.bookmark_buttons = function() {

    // process each posts at ListMode and ItemMode
    jQuery('div.post').each( function (i) {
            var post_titleLinkObj = jQuery(this).find('.post-title a');
            var post_title = encodeURIComponent(post_titleLinkObj.text());
            var post_url = encodeURIComponent(post_titleLinkObj.attr('href'));
            var post_desc = encodeURIComponent(jQuery(this).find('.post-body').text().substr(0,500));

            var buttonsHTML = '';
            for (var i=0; i < BloggerExtOptions.bookmark_buttons_bookmarklets.length; i++) {
                var img = BloggerExtOptions.bookmark_buttons_bookmarklets[i][0];
                var alt = BloggerExtOptions.bookmark_buttons_bookmarklets[i][1];
                var urlTempl = BloggerExtOptions.bookmark_buttons_bookmarklets[i][2];

                if (typeof BloggerExtOptions['bookmark_btn_'+alt] != 'undefined' && BloggerExtOptions['bookmark_btn_'+alt] == false) continue;

                var url = urlTempl.replace('%URL%', post_url, "gm");
                url = url.replace('%TITLE%', post_title, "gm");
                url = url.replace('%DESC%', post_desc, "gm");

                buttonsHTML += '<a class="bookmark_button" href="'+url+'" alt="'+alt+'"><img src="'+img+'" alt="'+alt+'"></a>';
            }
            jQuery(this).find('.post-footer .post-icons').after( BloggerExtOptions.bookmark_buttons_template.replace('%BOOKMARK_BUTTONS%', buttonsHTML, "gm"));
    });
};
BloggerExt.bookmark_buttons.user_pref = function() {
	var prefs = [];
	prefs.push({tab_name: 'bookmark', name: 'bookmark_buttons', display_name:'Add Bookmark:', data_type:'bool', 'default_value': BloggerExtOptions['bookmark_buttons'] });
  for (var i=0; i < BloggerExtOptions.bookmark_buttons_bookmarklets.length; i++) {
    var alt = BloggerExtOptions.bookmark_buttons_bookmarklets[i][1];
    prefs.push({tab_name: 'bookmark', name: 'bookmark_btn_'+alt, display_name: alt, data_type:'bool', 'default_value': BloggerExtOptions['bookmark_btn_'+alt] });
  }
	return prefs;
};
BloggerExt.bookmark_buttons.update_pref = function(prefs) {
  BloggerExtUserPrefs['bookmark_buttons'] = (typeof prefs['bookmark_buttons'] != 'undefined') ? true : false ;
  for (var i=0; i < BloggerExtOptions.bookmark_buttons_bookmarklets.length; i++) {
    var alt = BloggerExtOptions.bookmark_buttons_bookmarklets[i][1];
    var n =  'bookmark_btn_' + alt;
    BloggerExtUserPrefs[n] = (typeof prefs[n] != 'undefined') ? true : false ;
  }
};
BloggerExt.register('bookmark_buttons' ,  {
    bookmark_buttons: true,
    bookmark_buttons_version: 0.2,
    bookmark_buttons_template: '<span class="post-bookmarks">%BOOKMARK_BUTTONS%</span>',
    bookmark_buttons_bookmarklets: [
        [
          'http://blogger.ext2.googlepages.com/hemidemi.gif',
          'HEMiDEMi',
          'http://www.hemidemi.com/user_bookmark/new?via=sticker&url=%URL%&title=%TITLE%&description=%DESC%'
        ] ,
        [
          'http://blogger.ext2.googlepages.com/yahoo-myweb.png',
          'Yahoo',
          'http://tw.myweb2.search.yahoo.com/myresults/bookmarklet?ei=UTF-8&u=%URL%&t=%TITLE%&d=%DESC%'
        ],
        [
          'http://blogger.ext2.googlepages.com/myshare.gif',
          'MyShare',
          'http://myshare.url.com.tw/index.php?func=newurl&from=mysharepop&NewsMaster=1&url=%URL%&desc=%TITLE%&contents=%DESC%'
        ],
        [
        'http://funp.com/images/tools/post_05.gif',
        'funp',
        'http://funp.com/pages/submit/add.php?via=tools&ei=UTF-8&url=%URL%&s=%TITLE%&t=%DESC%'
        ],
        [
          'http://blogger.ext2.googlepages.com/delicious.png',
          'del.icio.us',
          'http://del.icio.us/post?url=%URL%&title=%TITLE%'
        ],
        [
          'http://blogger.ext2.googlepages.com/technorati.png',
          'Technotati',
          'http://technorati.com/faves?sub=%TITLE%&add=%URL%'
        ],
        [
          'http://blogger.ext2.googlepages.com/furl.gif',
          'FURL',
          'http://furl.net/storeIt.jsp?&u=%URL%&t=%TITLE%'
        ],
        [
          'http://blogger.ext2.googlepages.com/digg.png',
          'digg it!',
          'http://www.digg.com/submit?phase=2&url=%URL%&title=%TITLE%'
        ]
    ]
});
BloggerExt.registerHook(BloggerExt.bookmark_buttons);

